var serverUrl = 'http://www.xue114.com/api/'; //请求数据接口
var DOMINURL = 'http://www.xue114.com/';  // 域名地址
//检查是否为json字符串
function parseJson(str) {
	if (typeof str == 'string') {
		try {
			if (str.search(/^\{/) > -1 || str.search(/^\[/) > -1) {
				var obj = JSON.parse(str);
				return obj;
			} else {
				return false;
			}
		} catch (e) {
			return false;
		}
	} else if (typeof str == 'object') {
		return str;
	} else {
		throw new Error("params is not json type");
	}
}
//ajax请求
var query = function(url, params) {
	$.ajax({
		type: params.type && params.type.toUpperCase() || 'POST',
		url: serverUrl + url,
		data: params.data || {},
		dataType: "json",
		headers: {
			Accept: 'application/json',
			"Content-Type": "application/x-www-form-urlencoded"
		},
		success: function (res) {
			res = parseJson(res);
			if (res.errorMsg) {
				typeof params.success == "function" && params.success(res);
			} else {
				//接口请求返回错误的处理
				if (params.fail) {
					typeof params.fail == "function" && params.fail(res);
				} else {
					//接口请求错误的处理
					alert(res.errorMsg)
				}
			}
		},
		error: function (res) {
			if (params.error) {
				typeof params.error == "function" && params.error(res);
			} else {
				//接口请求错误的处理
				alert(res.errorMsg)
			}
		},
		complete: function (xhr, res) {
			//loading关闭
			typeof params.complete == "function" && params.complete(xhr, res);
		},
	});
};

// 关于IE8不支持Object.keys（）的处理
var DONT_ENUM =  "propertyIsEnumerable,isPrototypeOf,hasOwnProperty,toLocaleString,toString,valueOf,constructor".split(","),
hasOwn = ({}).hasOwnProperty;
for (var i in {
    toString: 1
}){
    DONT_ENUM = false;
}
Object.keys = Object.keys || function(obj){//ecma262v5 15.2.3.14
    var result = [];
    for(var key in obj ) if(hasOwn.call(obj,key)){
        result.push(key) ;
    }
    if(DONT_ENUM && obj){
        for(var i = 0 ;key = DONT_ENUM[i++]; ){
            if(hasOwn.call(obj,key)){
                result.push(key);
            }
        }
    }
    return result;
};
Array.prototype.notempty = function() {
    var re = [];
     for(var i = 0; i < this.length; i++) {       
		if (this[i] != '' && this[i] != undefined) {
			re.push(this[i])
		}
	}
	return re
}